use async_trait::async_trait;
use clap::Parser;

use crate::commands::commands::Command;
use crate::models::models;
use crate::models::models::command::CommandState;
use crate::utils::command;

#[derive(Debug, Default, Clone, Parser)]
#[clap(disable_help_flag = true)]
struct Args {
    // 字节长度
    #[arg(short = 'b', long = "bytes", default_value_t = false)]
    bytes: bool,
    value: Vec<String>,
}

#[derive(Debug, Default, Clone)]
pub(crate) struct LengthCommand {}

#[async_trait]
impl Command for LengthCommand {
    async fn execute(&self, command: models::command::Command) -> models::command::Command {
        let args = command::parse_command_args::<Args>(command.args.as_str());
        if args.is_err() {
            return self.save_command_output(command, CommandState::Success, args.unwrap_err().to_string());
        }

        let args = args.unwrap();
        if args.bytes {
            return self.save_command_output(command, CommandState::Success, format!("{}", args.value.join(" ").len()));
        }

        self.save_command_output(command, CommandState::Success, format!("{}", args.value.join(" ").chars().count()))
    }
}